/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.time;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.time.EleSourceTime;
import com.thinkgem.jeesite.modules.source.service.time.EleSourceTimeService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 时间资源管理Controller
 * @author zc
 * @version 2017-04-20
 */
@Controller
@RequestMapping(value = "${adminPath}/source/time/eleSourceTime")
public class EleSourceTimeController extends BaseController {

	@Autowired
	private EleSourceTimeService eleSourceTimeService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public EleSourceTime get(@RequestParam(required=false) String id) {
		EleSourceTime entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceTimeService.get(id);
		}
		if (entity == null){
			entity = new EleSourceTime();
		}
		return entity;
	}
	
	@RequiresPermissions("source:time:eleSourceTime:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceTime eleSourceTime, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());		
		}		
		eleSourceTime.setUsers(list);
		Page<EleSourceTime> page = eleSourceTimeService.findPage(new Page<EleSourceTime>(request, response), eleSourceTime); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		model.addAttribute("language", locale.getLanguage());
		return "modules/source/time/eleSourceTimeList";
	}

	@RequiresPermissions("source:time:eleSourceTime:view")
	@RequestMapping(value = "form")
	public String form(EleSourceTime eleSourceTime, Model model,HttpServletRequest request) {
		model.addAttribute("eleSourceTime", eleSourceTime);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		model.addAttribute("language", locale.getLanguage());
		return "modules/source/time/eleSourceTimeForm";
	}

	@RequiresPermissions("source:time:eleSourceTime:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceTime eleSourceTime, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, eleSourceTime)){
			return form(eleSourceTime, model,request);
		}
		eleSourceTimeService.save(eleSourceTime);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存时间资源成功";
		if(Global.US.equals(locale)){
			msg = "Time resource saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}
	
	@RequiresPermissions("source:time:eleSourceTime:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceTime eleSourceTime, RedirectAttributes redirectAttributes) {
		eleSourceTimeService.delete(eleSourceTime);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除时间资源成功";
		if(Global.US.equals(locale)){
			msg = "Time resource deleted";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}

	@RequiresPermissions("source:time:eleSourceTime:check")
	@RequestMapping(value = "check")
	public String check(EleSourceTime eleSourceTime, RedirectAttributes redirectAttributes) {
		eleSourceTimeService.check(eleSourceTime);
		addMessage(redirectAttributes, "审核时间资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}

}